package com.pl.rabarbar.old.stuff.need.to.be;

import javax.servlet.ServletContextEvent;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

import javax.servlet.ServletContextListener;

import com.pl.rabarbar.convert.ConvertJson;
import com.pl.rabarbar.services.GetFromServer;

public class Notificator extends TimerTask implements ServletContextListener {
	private Thread thread;
	private List idList;
	
	
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		this.thread.interrupt();

	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		thread = new Thread(this);
		thread.start();


	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (true) {

			try {
				String url = "http://localhost:9090/BookstoreClient/getyourbook/updatelist";

			    URL obj = new URL(url);
			    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
String responseFromServer = GetFromServer.getFromServer("http://localhost:8080/BookstoreTest/bookstore/getAllId");
			    // optional default is GET
			    con.setRequestMethod("POST");
			            con.setDoInput(true);
			            con.setDoOutput(true);
			            DataOutputStream dos = new DataOutputStream(con.getOutputStream());
			            dos.writeBytes(responseFromServer);
		
				TimeUnit.MINUTES.sleep(10);
			} catch (InterruptedException | IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			/*
			 * pobieranie danych co 10 minut
			 */
		}
		
	}

}
